package cn.glutnn.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;

import cn.glutnn.auth.application.entity.AuthUserDTO;
import cn.glutnn.auth.application.entity.Result;
import cn.glutnn.auth.domain.entity.AuthUserBO;
import cn.glutnn.auth.domain.service.AuthUserDomainService;
import cn.glutnn.auth.application.convert.AuthUserConverter;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Author:   msh
 * Date:     2025/1/13 下午4:54
 * Description:
 */

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private AuthUserDomainService authUserDomainService;

    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController register authUserDTO:{}", authUserDTO);
            }
            Preconditions.checkNotNull(authUserDTO.getUserName(), "用户名不能为空");
            Preconditions.checkNotNull(authUserDTO.getPassword(), "密码不能为空");
            Preconditions.checkNotNull(authUserDTO.getEmail(), "邮箱不能为空");
            AuthUserBO authUserBO = AuthUserConverter.INSTANCE.convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.register(authUserBO));
        } catch (Exception e) {
            log.error("UserController register error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    // 更新个人信息
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController update authUserDTO:{}", authUserDTO);
            }
            Preconditions.checkNotNull(authUserDTO.getId(), "用户id不能为空");
            AuthUserBO authUserBO = AuthUserConverter.INSTANCE.convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.update(authUserBO));
        } catch (Exception e) {
            log.error("UserController update error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    // 删除用户
    @RequestMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController delete authUserDTO:{}", authUserDTO);
            }
            Long id = authUserDTO.getId();
            Preconditions.checkNotNull(id, "用户id不能为空");
            AuthUserBO authUserBO = AuthUserConverter.INSTANCE.convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.delete(authUserBO));
        } catch (Exception e) {
            log.error("UserController delete error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    // 用户启用/禁用
    @RequestMapping("/changeStatus")
    public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController changeStatus authUserDTO:{}", authUserDTO);
            }
            Preconditions.checkNotNull(authUserDTO.getId(), "用户id不能为空");
            Preconditions.checkNotNull(authUserDTO.getStatus(), "用户状态不能为空");
            AuthUserBO authUserBO = AuthUserConverter.INSTANCE.convertDTOToBO(authUserDTO);
            return Result.ok(authUserDomainService.update(authUserBO));
        } catch (Exception e) {
            log.error("UserController changeStatus error:{}", e.getMessage(), e);
            return Result.fail("启用/禁用信息失败");
        }
    }

    // 会话登录接口
    @RequestMapping("/doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("validCode") String validCode) {
        try {
            Preconditions.checkArgument(StringUtils.isNotBlank(validCode), "验证码不能为空");
            SaTokenInfo saTokenInfo = authUserDomainService.doLogin(validCode);
            if (saTokenInfo == null) {
                return Result.fail("登录失败");
            }
            return Result.ok(saTokenInfo);
        } catch (Exception e) {
            log.error("UserController doLogin error:{}", e.getMessage(), e);
            return Result.fail("登录失败");
        }
    }

    // 获取用户信息
    @RequestMapping("/getUserInfo")
    public Result<AuthUserDTO> getUserInfo(@RequestBody AuthUserDTO authUserDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController getUserInfo authUserDTO:{}", authUserDTO);
            }
            Preconditions.checkNotNull(authUserDTO.getUserName(), "用户的openId不能为空");
            AuthUserBO authUserBO = AuthUserConverter.INSTANCE.convertDTOToBO(authUserDTO);
            AuthUserBO userInfo = authUserDomainService.getUserInfo(authUserBO);
            if (userInfo == null) {
                return Result.fail("获取用户信息失败");
            }
            return Result.ok(AuthUserConverter.INSTANCE.convertBOToDTO(userInfo));
        } catch (Exception e) {
            log.error("UserController getUserInfo error:{}", e.getMessage(), e);
            return Result.fail("获取用户信息失败");
        }
    }

    // 用户登出
    @RequestMapping("/logOut")
    public Result logOut(@RequestParam String userName) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController logOut userName:{}", userName);
            }
            Preconditions.checkNotNull(userName, "用户名不能为空");
            StpUtil.logout(userName);
            return Result.ok();
        } catch (Exception e) {
            log.error("UserController logOut error:{}", e.getMessage(), e);
            return Result.fail("登出失败");
        }
    }


    // 查询登录状态  ---- http://localhost:8081/acc/isLogin
    @RequestMapping("/isLogin")
    public SaResult isLogin() {
        return SaResult.ok("是否登录：" + StpUtil.isLogin());
    }

}
